package com.atguigu.gulimall.ware.listener;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.to.mq.OrderTo;
import com.atguigu.common.to.mq.StockDetailTo;
import com.atguigu.common.to.mq.StockLockedTo;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.entity.WareOrderTaskDetailEntity;
import com.atguigu.gulimall.ware.entity.WareOrderTaskEntity;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.gulimall.ware.vo.OrderVo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author ming
 * @create 2022/5/11
 * @description:
 */
@Slf4j
@Component
@RabbitListener(queues="stock.release.stock.queue")
public class StockReleaseListener {
    @Autowired
    private WareSkuService  wareSkuService;
    /**
     * 库存自动解锁
     * @param to
     * @param message
     */
    @RabbitHandler
    public void handlerStockLockRelease(StockLockedTo to, Message message, Channel channel) throws IOException {

        try {
              log.info("*****************收到解锁库存的消息***************");
            wareSkuService.unlockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            e.printStackTrace();
        }

    }
    @RabbitHandler()
    public void handlerOrderCloseRelease(OrderTo orderTo,Message message,Channel channel) throws IOException {
        try {
            log.info("*************订单取消准备解锁库存******************");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            wareSkuService.unlockStock(orderTo);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            e.printStackTrace();
        }
    }
}
